//
//  JkColor.swift
//  MobileRTCSwiftSample
//
//  Created by 张衡 on 2020/11/6.
//  Copyright © 2020 Zoom Video Communications, Inc. All rights reserved.
//

import Foundation

class JkColor: NSObject {
    
    static var colorList:[String:String] = [
        "A" : "#ffde00",
        "B" : "#ff5200",
        "C" : "#d50058",
        "D" : "#e60895",
        "E" : "#edd899",
        "F" : "#f4c400",
        "G" : "#f5b324",
        "H" : "#e18533",
        "I" : "#ea6953",
        "J" : "#b24c5a",
        "K" : "#e46486",
        "L" : "#9c182f",
        "M" : "#9c182f",
        "N" : "#73308b",
        "O" : "#410099",
        "P" : "#0082ca",
        "Q" : "#00a888",
        "R" : "#608eb5",
        "S" : "#608eb5",
        "T" : "#00577d",
        "U" : "#0081bc",
        "V" : "#006680",
        "W" : "#ffb700",
        "X" : "#75541d",
        "Y" : "#c57d5a",
        "Z" : "#7f3a2d",
        
        "1" : "#7adfa9",
        "2" : "#007935",
        "3" : "#94d600",
        "4" : "#799a05",
        "5" : "#81c8bd",
        "6" : "#006e63",
        "7" : "#4d86a0",
        "8" : "#33647f",
        "9" : "#6f3edb",
        "0" : "#401797",
    ]
    
    
    //#FFFFFF
    class func first(with:String) -> NSColor? {
        if let value = self.colorList.first(where: {$0.key==with}) {
            let color = NSColor.init(hexString: value.value)
            return color
        }
        return nil
    }
}

extension String {
    
    //获取拼音首字母（大写字母）
    func firstLetter() -> String {
        //转变成可变字符串
        let mutableString = NSMutableString.init(string: self)
        //将中文转换成带声调的拼音
        CFStringTransform(mutableString as CFMutableString, nil,kCFStringTransformToLatin, false)
        //去掉声调
        let pinyinString = mutableString.folding(options:          String.CompareOptions.diacriticInsensitive, locale:NSLocale.current)
        //将拼音首字母换成大写
        let strPinYin = self.polyphoneStringHandle(pinyinString: pinyinString).uppercased()
        //截取大写首字母
        let firstString = strPinYin.substring(to:strPinYin.index(strPinYin.startIndex, offsetBy: 1))
        //判断首字母是否为大写
        let regexA = "^[A-Z1-9]$"
        let predA = NSPredicate.init(format: "SELF MATCHES %@", regexA)
        return predA.evaluate(with: firstString) ? firstString : "#"
    }
    
    //多音字处理，根据需要添自行加
    func polyphoneStringHandle(pinyinString: String) -> String {
        let pinyins:[String:String] = [
            "长":"chang",
            "沈":"shen",
            "厦":"xia",
            "地":"di",
            "重":"chong",
        ]
        if let first = pinyins.first(where: {self.hasPrefix($0.key)}) {
            return first.value
        }
        return pinyinString
    }
}

